package com.beijiteshop.shop.ui.order.fragment

import android.content.Context
import android.os.Bundle
import android.util.TypedValue
import android.view.View
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.beijiteshop.libcommon.ext.databinding.bindView
import com.beijiteshop.shop.R
import com.beijiteshop.shop.base.BaseContainerFragment
import com.beijiteshop.shop.databinding.FragmentOrderTabBinding
import com.beijiteshop.shop.ui.main.FragmentPagerAdapter
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView

/**
 * @Author LiaoZhongKai
 * @Date 2021/3/11 11:07
 * @Description
 */
class OrderTabFragment : BaseContainerFragment(R.layout.fragment_order_tab) {

    companion object{

        const val BUNDLE_DEFAULT_INDEX = "bundle_default_index"

        fun newInstance(index: Int): Fragment{
            return OrderTabFragment().apply {
                arguments = Bundle().apply {
                    putInt(BUNDLE_DEFAULT_INDEX,index)
                }
            }
        }
    }

    private val mBinding by bindView<FragmentOrderTabBinding>()
    private val mTitles = mutableListOf<String>()
    private val mFragmentList = mutableListOf<Fragment>()
    private var mSelectIndex = 0//默认选中的tab

    override fun onLazyLoad() {

    }

    override fun initView(view: View) {
        mSelectIndex = arguments?.getInt(BUNDLE_DEFAULT_INDEX)?:0
        mTitles.addAll(resources.getStringArray(R.array.order_tabs))
        mTitles.forEachIndexed { index, _ ->
            mFragmentList.add(OrderListFragment.newInstance(index))
        }

        mBinding.orderTabTitleBar.titleBarTitleTv.text = resources.getString(R.string.my_order)

        initIndicator()
        initViewPager()
    }

    override fun initData() {

    }

    override fun initEvent() {
        mBinding.orderTabTitleBar.titleBarBackIv.setOnClickListener {
            close()
        }
    }

    private fun initIndicator(){
        mBinding.orderTabIndicator
            .apply {
                val commonNavigator = CommonNavigator(requireContext())
                    .apply {
                        adapter = object : CommonNavigatorAdapter() {
                            override fun getTitleView(
                                context: Context?,
                                index: Int
                            ): IPagerTitleView {
                                val simplePagerTitleView: SimplePagerTitleView =
                                    ColorTransitionPagerTitleView(context)
                                simplePagerTitleView.text = mTitles[index]
                                simplePagerTitleView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14f)
                                simplePagerTitleView.normalColor = ContextCompat.getColor(
                                    requireContext(),
                                    R.color.text_light_dark
                                )
                                simplePagerTitleView.selectedColor = ContextCompat.getColor(
                                    requireContext(),
                                    R.color.color_primary
                                )
                                simplePagerTitleView.setOnClickListener {
                                    mBinding.orderTabVp.setCurrentItem(index,false)
                                }
                                return simplePagerTitleView
                            }

                            override fun getCount(): Int {
                                return mTitles.size
                            }

                            override fun getIndicator(context: Context?): IPagerIndicator {
                                val indicator = LinePagerIndicator(context)
                                indicator.mode = LinePagerIndicator.MODE_WRAP_CONTENT
                                indicator.setColors(
                                    ContextCompat.getColor(
                                        requireContext(),
                                        R.color.color_primary
                                    )
                                )
                                return indicator
                            }
                        }
                    }
                this.navigator = commonNavigator
                ViewPagerHelper.bind(this, mBinding.orderTabVp)
            }
    }

    private fun initViewPager(){
        mBinding.orderTabVp.apply {
            adapter = FragmentPagerAdapter(mFragmentList,childFragmentManager)
            currentItem = mSelectIndex
        }
    }

}